<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Generic controller for entity class, CI AR.
 * @author hmQuan
 *
 */
class Offer extends MY_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('t_offer');
	}

	/**
	 * required_offer : user required offer a product
	 * @return [type] [description]
	 */
	public function request_offer(){
		// validation post
		$post = parent::validation_user();
		$dataOffer = array();
		// gcm reg id
		$gcm_regid = $post[GCM_REGID];
		// user booking
		$dataOffer[OFFER_USER_ID] = $post[T_USERS][USER_ID];

		// validation product
		if(!isset($post[OFFER_PRODUCT_ID]) || empty($post[OFFER_PRODUCT_ID])){
			parent::print_result('Missing key or value ' . OFFER_PRODUCT_ID);exit();
		}else{
			$productId = $post[OFFER_PRODUCT_ID];
			// retrieve product
			$this->load->model('t_product');
			$product = $this->t_product->get_data_by_id($productId);
			if(empty($product)){
				parent::print_result('Product does not exist');exit();
			}else if($product[0][PRODUCT_OFFER_ID] >= 1 ){
				parent::print_result('Product has been sold');exit();
			}

			$dataOffer[OFFER_PRODUCT_ID] = $productId;
		}

		// validation price
		if(!isset($post[OFFER_PRICE]) || empty($post[OFFER_PRICE])){
			parent::print_result('Missing key or value ' . OFFER_PRICE);exit();
		}else{
			$queryOffer = $this->t_offer->get_data_by_property($dataOffer);
			// var_dump($queryOffer);
			if($queryOffer == null || count($queryOffer) == 0){
				$dataOffer[OFFER_PRICE] = $post[OFFER_PRICE];
				$offer = $this->t_offer->set_data($dataOffer, 1);
				if($offer != null && count($offer) > 0){
					parent::print_result($offer[0], SUCCESS_VALUE);
				}else{
					parent::print_result('Offer Failed');
				}
			}else{
				$queryOffer[0][OFFER_PRICE] = $post[OFFER_PRICE];
				// var_dump($queryOffer);
				// update price offer
				$this->t_offer->update_data_by_id(array(OFFER_PRICE => $post[OFFER_PRICE]), $queryOffer[0][OFFER_ID]);
				
				$this->load->model('t_product');
				$productOwn = $this->t_product->get_data_by_id($productId);
				parent::push_notification_to_client_via_user_id($productOwn[0][PRODUCT_USER_ID], $post[T_USERS][USER_NAME] . PUSH_REQUEST_OFFER, PUSH_REQUEST_OFFER_TO_OWN);
				parent::print_result($queryOffer[0], SUCCESS_VALUE);
			}
		}
		exit();
	}

	public function remove_offer(){
		$post = parent::validation_user();

		if(!isset($post[OFFER_ID])){
			parent::print_result('Missing key or value ' . OFFER_ID);exit();
		}else{
			$offerId = $post[OFFER_ID];
			$queryOffer = $this->t_offer->unset_data_by_id($offerId);
			parent::print_result('Remove success', SUCCESS_VALUE);exit();
		}
	}

	public function retrieve_infor_offering_by_product(){
		$post = parent::validation_user();

		if(!isset($post[OFFER_PRODUCT_ID])){
			parent::print_result('Missing key or value '. OFFER_PRODUCT_ID);exit();
		}else{
			$productId = $post[OFFER_PRODUCT_ID];
		}

		$arrOffering = $this->t_offer->get_offering($productId);
		if(empty($arrOffering)){
			parent::print_result('No offer',SUCCESS_VALUE);exit();
		}else{
			// $arrUser = array();
			// foreach ($arrOffering as $key => $offer) {
			// 	$arrUser[] = $offer[OFFER_USER_ID];
			// }

			parent::print_result($arrOffering, SUCCESS_VALUE);exit();
		}

	}

	public function history_offer(){
		$post = parent::validation_user();
		$user_id = $post[T_USERS][USER_ID];

		$history_offer = $this->t_offer->get_history_offer($user_id);

		if(empty($history_offer)){
			parent::print_result(0);exit();
		}else{
			parent::print_result($history_offer,SUCCESS_VALUE);exit();
		}
	}
}